﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKhachSan.BUS;
using QuanLyKhachSan.DAO;
using QuanLyKhachSan.DTO;
using COMExcel = Microsoft.Office.Interop.Excel;

namespace QuanLyKhachSan
{
    public partial class frmLapHoaDon : Form
    {
        public frmLapHoaDon()
        {
            InitializeComponent();
        }
        //bien luu he thong hien tai
        private System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;

        private void frmLapHoaDon_Load(object sender, EventArgs e)
        {
            //Lấy giờ hệ thống
            string gio = DateTime.Now.ToLongTimeString();
            string ngay = DateTime.Now.ToLongDateString();
            string chuoi = ngay + " " + gio;
            txtNgayHienTai.Text = chuoi;
            btnThanhToan.Enabled = false;
            btnHuy.Enabled = false;

            //su dung he thong en-US
            //oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            
        }
        private void btnThemKH_Click(object sender, EventArgs e)
        {
            frmQuanLyKhachHang frm = new frmQuanLyKhachHang();
            frm.ShowDialog();
        }
        void LoadKhiXong()
        {
            if (dgvDSKHChon.Rows.Count > 0)
            {
                dgvDSKHChon.Rows.RemoveAt(0);
            }
            if (dgvDSPhongTra.Rows.Count > 0)
            {
                for (int k = j-1; k >=0; k--)
                {
                    dgvDSPhongTra.Rows.RemoveAt(k);
                }
            }
            phaiTra = 0;
            j = 0;
            i = 0;
            
            txtTenKhachHang.Text = "";
            dgvDSKH.DataSource = null;
            txtPhong.Text = "";
            dgvDSPhongDaThue.DataSource = null;
            txtTongTien.Text = phaiTra.ToString();
            btnThanhToan.Enabled = false;
            btnHuy.Enabled = false;
          
        }
        private void GhiFileEXCEL()
        {
            string khachHang = dgvDSKHChon.Rows[0].Cells["khachhang"].Value.ToString();
            string diaChi = dgvDSKHChon.Rows[0].Cells["diachi"].Value.ToString();
            DateTime ngayThanhToan = DateTime.Now;
            string triGiaTien = txtTongTien.Text;

            // Khởi động chtr Excell
            COMExcel.Application exApp = new COMExcel.Application();

            exApp.Visible = false;
            // Thêm file temp xls
            COMExcel.Workbook exBook = exApp.Workbooks.Add(COMExcel.XlWBATemplate.xlWBATWorksheet);

            // Lấy sheet 1.
            COMExcel.Worksheet exSheet = (COMExcel.Worksheet)exBook.Worksheets[1];

            COMExcel.Range r = (COMExcel.Range)exSheet.Cells[1, 1];
            COMExcel.Range r2 = (COMExcel.Range)exSheet.Cells[1, 5];

            // Ghi dữ liệu loi ngo
            r.Value2 = "HÓA ĐƠN THANH TOÁN";
            r.Font.Bold = true;
            r.Font.Size = 16;
            r.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            r.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
            exApp.get_Range(r, r2).Merge(Type.Missing);
            // Giãn cột
            r.Columns.AutoFit();
            r.Rows.RowHeight = 30;

            COMExcel.Range rKH = (COMExcel.Range)exSheet.Cells[2, 1];
            COMExcel.Range rKH2 = (COMExcel.Range)exSheet.Cells[2, 3];
            rKH.Value2 = "Khách Hàng/Cơ Quan: " + khachHang;
            rKH.Font.Bold = true;
            rKH.Font.Size = 13;
            exApp.get_Range(rKH, rKH2).Merge(Type.Missing);
            rKH.Columns.AutoFit();

            COMExcel.Range rDC = (COMExcel.Range)exSheet.Cells[2, 4];
            COMExcel.Range rDC2 = (COMExcel.Range)exSheet.Cells[2, 5];
            rDC.Value2 = "Địa chỉ: " + diaChi;
            rDC.Font.Bold = true;
            rDC.Font.Size = 13;
            exApp.get_Range(rDC, rDC2).Merge(Type.Missing);
            rDC.Columns.AutoFit();

            COMExcel.Range rNgay = (COMExcel.Range)exSheet.Cells[3, 1];
            COMExcel.Range rNgay2 = (COMExcel.Range)exSheet.Cells[3, 3];
            rNgay.Value2 = "Ngày thanh toán: " + ngayThanhToan.ToLongDateString();
            rNgay.Font.Bold = true;
            rNgay.Font.Size = 13;
            exApp.get_Range(rNgay, rNgay2).Merge(Type.Missing);
            rNgay.Columns.AutoFit();

            COMExcel.Range rTriGia = (COMExcel.Range)exSheet.Cells[3, 4];
            COMExcel.Range rTriGia2 = (COMExcel.Range)exSheet.Cells[3, 5];
            rTriGia.Value2 = "Trị Giá: " + triGiaTien;
            rTriGia.Font.Bold = true;
            rTriGia.Font.Size = 13;
            exApp.get_Range(rTriGia, rTriGia2).Merge(Type.Missing);
            rTriGia.Columns.AutoFit();

            //dinh dang tieu de
            COMExcel.Range range = exApp.get_Range(exSheet.Cells[4, 1], exSheet.Cells[4, 5]);
            range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            //range.Value2 = "STT";
            range.Font.Bold = true;
            range.Font.Size = 12;
            range.Columns.ColumnWidth = 15;

            COMExcel.Range rSTT = (COMExcel.Range)exSheet.Cells[4, 1];
            rSTT.Value2 = "STT";

            COMExcel.Range rPhong = (COMExcel.Range)exSheet.Cells[4, 2];
            rPhong.Value2 = "PHòng";

            COMExcel.Range rSoNgay = (COMExcel.Range)exSheet.Cells[4, 3];
            rSoNgay.Value2 = "Số Ngày Thuê";

            COMExcel.Range rDonGia = (COMExcel.Range)exSheet.Cells[4, 4];
            rDonGia.Value2 = "Đơn Giá";

            COMExcel.Range rThanhTien = (COMExcel.Range)exSheet.Cells[4, 5];
            rThanhTien.Value2 = "Thành Tiền";
            for (int i = 0; i < dgvDSPhongTra.Rows.Count; i++)
            {
                COMExcel.Range rDuLieuSTT = (COMExcel.Range)exSheet.Cells[i + 5, 1];
                rDuLieuSTT.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuSTT.Value2 = (i + 1);

                COMExcel.Range rDuLieuPhong = (COMExcel.Range)exSheet.Cells[i + 5, 2];
                rDuLieuPhong.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuPhong.Value2 = dgvDSPhongTra.Rows[i].Cells["phongTra"].Value.ToString();

                COMExcel.Range rDuLieuSoNgay = (COMExcel.Range)exSheet.Cells[i + 5, 3];
                rDuLieuSoNgay.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuSoNgay.Value2 = dgvDSPhongTra.Rows[i].Cells["soNgayThue"].Value.ToString();

                COMExcel.Range rDuLieuDonGia = (COMExcel.Range)exSheet.Cells[i + 5, 4];
                rDuLieuDonGia.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuDonGia.Value2 = dgvDSPhongTra.Rows[i].Cells["donGia"].Value.ToString();

                COMExcel.Range rDuLieuThanhTien = (COMExcel.Range)exSheet.Cells[i + 5, 5];
                rDuLieuThanhTien.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuThanhTien.Value2 = dgvDSPhongTra.Rows[i].Cells["thanhTien"].Value.ToString();
            }
            exApp.Visible = true;
        }
        private void btnThanhToan_Click(object sender, EventArgs e)
        {
            if (dgvDSKHChon.Rows.Count == 0)
            {
                MessageBox.Show("Ban chua chon khach hang thanh toan", "Thong bao");
                return;
            }
            if (dgvDSPhongTra.Rows.Count == 0)
            {
                MessageBox.Show("Ban chua chon phong thanh toan", "Thong bao");
                return;
            }
            DialogResult result = MessageBox.Show("Ban chac chan muon thanh toan khong?", "Thong Bao", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                HoaDonDTO hoaDon = new HoaDonDTO();
                hoaDon.NgayLapHoaDon = DateTime.Parse(DateTime.Now.ToLongDateString());
                hoaDon.TongTien = float.Parse(txtTongTien.Text.ToString());
                int ma = int.Parse(dgvDSKHChon.Rows[0].Cells["ma"].Value.ToString());
                hoaDon.MaKhachHang = ma;
                HoaDonBUSResult ok = HoaDonBUS.Insert(hoaDon);
                ChiTietHoaDonDTO chiTietHoaDon = new ChiTietHoaDonDTO();
                int tongSoPhong = dgvDSPhongTra.Rows.Count;
                DataTable dt = HoaDonBUS.LayMaHoaDonMoiThem();
                DataRow dr = dt.Rows[0];
                                
                for (int h = 0; h < tongSoPhong; h++)
                {
                    PhongBUS.TraPhong(int.Parse(dgvDSPhongTra.Rows[h].Cells["maPhong"].Value.ToString()));
                    chiTietHoaDon.MaHoaDon = int.Parse(dr[0].ToString());
                    DataTable data = PhieuThuePhongBUS.LayMaPhieuThueTheoMaPhong(int.Parse(dgvDSPhongTra.Rows[h].Cells["maPhong"].Value.ToString()));
                    DataRow row = data.Rows[0];
                    chiTietHoaDon.MaPhieuThue = int.Parse(row[0].ToString());
                    chiTietHoaDon.SoNgayThue = int.Parse(dgvDSPhongTra.Rows[h].Cells["soNgayThue"].Value.ToString());
                    chiTietHoaDon.DonGia = float.Parse(dgvDSPhongTra.Rows[h].Cells["donGia"].Value.ToString());
                    ChiTietHoaDonBUS.Insert(chiTietHoaDon);

                }
                if (ok == HoaDonBUSResult.OK)
                {
                    DialogResult dlr = MessageBox.Show("Lap Hoa Don Thanh Cong\nBan Co Muon Xuat Ra File EXCEL Khong?", "Thong Bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dlr == DialogResult.Yes)
                    {
                        GhiFileEXCEL();
                    }
                }
                LoadKhiXong();
                
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            //LoadKhiXong();
            dgvDSKHChon.Rows.Clear();
            dgvDSPhongTra.Rows.Clear();
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtTenKhachHang_TextChanged(object sender, EventArgs e)
        {
            dgvDSKH.DataSource = KhachHangBUS.LayDSKhachHangTheoTen(txtTenKhachHang.Text);
        }

        private void txtPhong_TextChanged(object sender, EventArgs e)
        {
            dgvDSPhongDaThue.DataSource = PhongBUS.LayDanhSachPhongDaThue(txtPhong.Text);
        }
        int i = 0;
        private void btnChonKH_Click(object sender, EventArgs e)
        {
            if (dgvDSKH.SelectedRows.Count == 0)
            {
                MessageBox.Show("Vui long chon khach hang thanh toan", "Thong bao");
                return;
            }
            else
            {
                if (i == 1)
                {
                    MessageBox.Show("Moi hoa don chi mot khach hang thanh toan", "Thong bao");
                    return;
                }
                btnThanhToan.Enabled = true;
                btnHuy.Enabled = true;
                int vt = dgvDSKH.SelectedRows[0].Index;
                DataGridViewRow dr = dgvDSKH.Rows[vt];
                dgvDSKHChon.Rows.Add();
                dgvDSKHChon.Rows[i].Cells["stt"].Value = 1;
                dgvDSKHChon.Rows[i].Cells["ma"].Value = dr.Cells[0].Value.ToString();
                dgvDSKHChon.Rows[i].Cells["khachhang"].Value = dr.Cells[1].Value.ToString();
                dgvDSKHChon.Rows[i].Cells["loaikhachhang"].Value = dr.Cells[2].Value.ToString();
                dgvDSKHChon.Rows[i].Cells["cmnd"].Value = dr.Cells[3].Value.ToString();
                dgvDSKHChon.Rows[i].Cells["diachi"].Value = dr.Cells[4].Value.ToString();
                i++; 
            }
        }
        int j = 0;
        double phaiTra = 0;
        private bool KiemTraPhongTra()
        {
            int vt = dgvDSPhongDaThue.SelectedRows[0].Index;
            DataGridViewRow dr = dgvDSPhongDaThue.Rows[vt];
            for (int r = 0; r < dgvDSPhongTra.Rows.Count; r++)
            {
                if (dgvDSPhongTra.Rows[r].Cells["maPhong"].Value.ToString() == dr.Cells[0].Value.ToString())
                {
                    MessageBox.Show("Phong da trong bang thanh toan", "Thong bao");
                    return false;
                }
            }
            return true;
        }
        private void btnChonPhong_Click(object sender, EventArgs e)
        {
            if (dgvDSPhongDaThue.SelectedRows.Count == 0)
            {
                MessageBox.Show("Vui long chon phong can tra", "Thong bao");
                return;
            }
            else
            {
                btnThanhToan.Enabled = true;
                btnHuy.Enabled = true;
                int vt = dgvDSPhongDaThue.SelectedRows[0].Index;
                DataGridViewRow dr = dgvDSPhongDaThue.Rows[vt];
                if (dgvDSPhongTra.Rows.Count == 0)
                {
                    dgvDSPhongTra.Rows.Add();
                    dgvDSPhongTra.Rows[j].Cells["sttPhongTra"].Value = j + 1;
                    dgvDSPhongTra.Rows[j].Cells["maPhong"].Value = dr.Cells[0].Value.ToString();
                    dgvDSPhongTra.Rows[j].Cells["phongTra"].Value = dr.Cells[1].Value.ToString();
                    dgvDSPhongTra.Rows[j].Cells["soNgayThue"].Value = dr.Cells[2].Value.ToString();
                    dgvDSPhongTra.Rows[j].Cells["donGia"].Value = dr.Cells[3].Value.ToString();
                    dgvDSPhongTra.Rows[j].Cells["thanhTien"].Value = dr.Cells[4].Value.ToString();
                    double tien = double.Parse(dr.Cells[4].Value.ToString());
                    phaiTra += tien;
                    j++;
                }
                else
                {
                    if (KiemTraPhongTra() == false)
                    {
                        return;
                    }
                    else
                    {
                        dgvDSPhongTra.Rows.Add();
                        dgvDSPhongTra.Rows[j].Cells["sttPhongTra"].Value = j + 1;
                        dgvDSPhongTra.Rows[j].Cells["maPhong"].Value = dr.Cells[0].Value.ToString();
                        dgvDSPhongTra.Rows[j].Cells["phongTra"].Value = dr.Cells[1].Value.ToString();
                        dgvDSPhongTra.Rows[j].Cells["soNgayThue"].Value = dr.Cells[2].Value.ToString();
                        dgvDSPhongTra.Rows[j].Cells["donGia"].Value = dr.Cells[3].Value.ToString();
                        dgvDSPhongTra.Rows[j].Cells["thanhTien"].Value = dr.Cells[4].Value.ToString();
                        double tien = double.Parse(dr.Cells[4].Value.ToString());
                        phaiTra += tien;
                        j++;
                    }
                }
            }
            txtTongTien.Text = phaiTra.ToString();
        }

        private void btnHuyChonKH_Click(object sender, EventArgs e)
        {
            if (dgvDSKHChon.Rows.Count == 0)
            {
                return;
            }
            i--; 
            dgvDSKHChon.Rows.Clear();
        }

        private void btnHuyChonPhong_Click(object sender, EventArgs e)
        {
            if (dgvDSPhongTra.Rows.Count == 0)
            {
                return;
            }
            int vt = dgvDSPhongTra.SelectedRows[0].Index;
            double tien = double.Parse(dgvDSPhongTra.Rows[vt].Cells["thanhTien"].Value.ToString());
            phaiTra = phaiTra - tien;
            txtTongTien.Text = phaiTra.ToString();
            dgvDSPhongTra.Rows.RemoveAt(vt);
            j--;
        }

        private void frmLapHoaDon_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
        }  
    }
}